const globalIp = getApp().globalData.userIp
Page({
  /**
   * 页面的初始数据
   */
  data: {
    scrollTop: 0,
    searchResult: false,
    screen:"客户选择",
    screenCode: '',
    screenFlag: false,//控制综合下拉框是否显示
    screenColor: false,
    dataColor: false,
    dataTime:"日期选择",
    dataTime1:"起始日期",
    dataTime2:"截止日期",
    dataFlag: false, //控制综合下拉框是否显示
    isShowAll:false,
    projectList:[],
    inputShowed: false,
    inputVal: "",
    openid:''
  },
  showAll(){
    if(this.data.isShowAll){
      this.setData({
        isShowAll:false
      })
    }else{
      this.setData({
        isShowAll:true
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      search: this.search.bind(this),
    })   
  },
  // 搜索框获取客户信息
  search: function (value) {
    var that = this;
    var cardInfo = [];
    var result = new Object();
    result.text = '';
    result.value = '';
    var resultList=[];
    wx.request({
      url: globalIp + 'home/ocrd/getCardInfo',
      data: {
        value: value,
        openId: that.data.openid || wx.getStorageSync('userInfo').openid,
        login: wx.getStorageSync('userInfo').login || 'N'
      },
      header: {
        'content-type': 'application/json'
      },
      method: 'POST',
      success: function(res) {
        cardInfo = res.data.cardInfo.data
        console.log(res.data.cardInfo.data)
      },
      fail: function(res) {
        console.log("请求失败")
      }
    })
    return new Promise((resolve, reject) => {
      setTimeout(() => {
          // resolve([{text: '搜索结果', value: 1}, {text: '搜索结果2', value: 2}])
          if (cardInfo) {
            for (var i of cardInfo) {
              result.text = i.CardName;
              result.value = i.CardCode;
              resultList.push(result);
              result = {};
            }
          }
          resolve(resultList);
      }, 200)
    })      
  },
  selectResult: function (e) {
    console.log('select result', e.detail)
    this.setData({
      screenFlag: false,
      screenColor:true,
      screen: e.detail.item.text,
      screenCode: e.detail.item.value
    })
    this.getProject()
  },

  changeDate1(e) {
    console.log(e)
    this.setData({
      dataTime1: e.detail.value,
      // dataFlag: false,
      dataColor: true
    })
    // this.getProject()
  },
  changeDate2(e) {
    this.setData({
      dataTime2: e.detail.value,
      dataFlag: false,
      dataColor: true
    })
    this.getProject()
  },

  dataShow: function () {//日期下拉
    if (this.data.dataFlag){
      this.setData({
        dataFlag : !this.data.dataFlag,
        screenFlag: false,
        screenColor: false
      })
    }else{
      this.setData({
        dataFlag: !this.data.dataFlag,
        screenFlag: false,
        screenColor: false
      })
    }
  },

  screenShow: function () {//综合下拉
    if (this.data.screenFlag){
      this.setData({
        screenFlag : !this.data.screenFlag,
        dataFlag: false,
        dataColor: false
      })
    }else{
      this.setData({
        screenFlag: !this.data.screenFlag,
        dataFlag: false,
        dataColor: false
      })
    }
  },
  handletouchtart: function (event) {//点击透明背景隐藏下拉
    this.setData({
      screenFlag: false,
      dataFlag: false
    })
  },
  
  // 统一请求
  getProject(e) {
    let that = this;
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    wx.request({
      url: globalIp + 'home/ordr/showProjectForUser',
      data: {
        cardCode: that.data.screenCode,
        sdate: that.data.dataTime1 == '起始日期' ? '' : that.data.dataTime1,
        edate: that.data.dataTime2 == '截止日期' ? '' : that.data.dataTime2,
        openId: that.data.openId || wx.getStorageSync('userInfo').openid,
        login: wx.getStorageSync('userInfo').login || 'N'
      },
      header: {
        'content-type': 'application/json'
      },
      method: 'POST',
      success: function(res) {
        if (res.data.jsonObject) {
          var data = res.data.jsonObject.data;
          var arr = [];
          var res = [];
          for (var i in data) {
            arr[i] = [];
            for(var j in data[i]) {
              arr[i].push(data[i][j]);
            }
          }
          for (var i = 0; i < arr[0].length; ++i) {
            res[i] = [];
            for (var j = 0; j < arr.length; ++j) {
              res[i][j] = arr[j][i];
            }
          }
          // console.log(res)
          that.setData({
            projectList: res,
            scrollTop: 80
          })
        } else {
          that.setData({
            scrollTop: 80
          })
        }
      },
      fail: function(res) {
        wx.showToast({
          title: '请返回上一页面，重新请求',
          icon: 'none',
          duration: 2000,
        })
      },
      complete: function(res) {
        wx.hideLoading()
      }
    })
  }
 })
